/*
 * list_bomb.c
 *
 *  Created on: 15 nov. 2010
 *      Author: Nicolas Aucouturier, Laurent R�veill�re
 */

//#include <time.h>
#include "../include/game.h"
#include "../include/map.h"
#include "../include/player.h"
#include "../include/misc.h"
#include "../include/level.h"
#include "../include/bomb.h"
#include "../include/list_bomb.h"

// FIXME
struct s_bomb {
	int x, y;
	int power;
	Uint32 time_init;
	int time;
	SDL_Surface * length_img[9];
};

struct s_listbomb {
	t_bomb bomb;
	t_listbomb next;
};

t_listbomb
list_new(void) {
	t_listbomb l = malloc(sizeof(t_listbomb));
	l->bomb = NULL;
	l->next = NULL;
	return l;
}

t_listbomb
list_add(t_listbomb l, t_bomb bomb) {
	t_listbomb save = l; // save

	if(list_is_empty(l)) {
		l->bomb = bomb;
		return l;
	}

	while(l->next != NULL) {
		l = l->next;
	}
	t_listbomb last = list_new();
	last->bomb = bomb;

	l->next = last;
	l = save; // restore
	return save;
}

int
list_is_empty(t_listbomb l) {
	return l->bomb == NULL;
}

void
list_print(t_listbomb l) {
	t_listbomb save = l; // save
	while(l != NULL && l->bomb != NULL) {
		fprintf(stderr, "[BOMB: (%d, %d) power %d, time %u] -> ",
				l->bomb->x, l->bomb->y, l->bomb->power, l->bomb->time);
		l = l->next;
	}
	l = save; // restore
	fprintf(stderr, "();\n");
}

void
list_free(t_listbomb l) {
	t_listbomb save = l; // save
		while(l != NULL && l->bomb != NULL) {
			if(l->bomb->time==5000)
			{
			fprintf(stderr, "[BOMB delete: (%d, %d)] -> ",
					l->bomb->x, l->bomb->y);
			l = l->next;
			}
		}
		l = save; // restore
}

/*
 t_bomb set_bomb(int x,int y,float timer, int power)
 {

 t_bomb bomb = malloc(sizeof(*bomb));
 if(!bomb)
 error("Memory error");

 bomb->directed_img_bomb[0] = load_image(IMG_BOMB_TTL1);//Load image
 bomb->directed_img_bomb[1] = load_image(IMG_BOMB_TTL2);
 bomb->directed_img_bomb[2] = load_image(IMG_BOMB_TTL3);
 bomb->directed_img_bomb[4] = load_image(IMG_BOMB_TTL4);

 bomb->timer = timer;
 bomb->power = power;
 bomb->bomb_x=x;
 bomb->bomb_y=y;
 return bomb;
 }
 */
/*
 void bomb_display(t_bomb bomb, SDL_Surface *screen) {
 assert(bomb);
 assert(screen);

 SDL_Rect bomb_place;
 bomb_place.x = bomb->bomb_x * SIZE_BLOC;
 bomb_place.y = bomb->bomb_y * SIZE_BLOC;

 SDL_BlitSurface(player->directed_img[player->current_way], NULL, screen,
 &place);
 }
 */
/*
void bomb_display(t_bomb bomb, SDL_Surface *screen) {
	assert(bomb);
	assert(screen);
	SDL_Rect bomb_place;

	//bomb_place.x = bomb->bomb_x * SIZE_BLOC;
	//bomb_place.y = bomb->bomb_y * SIZE_BLOC;
	// Here we set the time for the bomb to explode and the sprites associated

	//float timer;
	//float one_second = (1000/30);
	//timer=bomb->timer;
	//if (timer >= 3*one_second )
	{
	//	SDL_BlitSurface(bomb->directed_img_bomb[3], NULL, screen, &bomb_place);
		//bomb->timer -= 1;
	}

	 if ((timer >= 2 * one_second) && (timer < 3 * one_second) )
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[2], NULL, screen, &bomb_place);
	 bomb->timer -= 1;
	 }

	 if ( (timer >= 1 * one_second) && (timer < 2 * one_second) )
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[1], NULL, screen, &bomb_place);
	 bomb->timer -= 1;
	 }

	 if ((timer < 1 * one_second) && (timer >=0) )
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[0], NULL, screen, &bomb_place);
	 bomb->timer -= 1;
	 }


	 if ((timer < 0) && (timer >= -1 * (one_second) ))
	 {
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 int explosion_range;
	 for (explosion_range=0 ; bomb->power >= explosion_range; explosion_range++ )
	 {
	 bomb_place.x = (bomb ->bomb_x) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y + explosion_range) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 bomb_place.x = (bomb ->bomb_x) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y - explosion_range) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 bomb_place.x = (bomb ->bomb_x + explosion_range) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y ) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 bomb_place.x = (bomb ->bomb_x - explosion_range) * SIZE_BLOC;
	 bomb_place.y = (bomb ->bomb_y ) * SIZE_BLOC;
	 SDL_BlitSurface (bomb->directed_img_bomb[4], NULL, screen, &bomb_place);
	 }
	 }

}
*/


/*
int bomb_can_be_put (t_map map, int bomb_x, int bomb_y) {

	switch (map_get_cell_type(map, bomb_x, bomb_y)) {

	case CELL_TREE:
		return 0;
	break;

	case CELL_CASE:
		return 0;
	break;

	case CELL_MONSTER:
		return 0;
	break;

	default:
		return 1;
	break;
	}
	return 1;
}
*/

/*
 //Initialisation de la liste

 list_bomb bombeInitialisation()
 {

 list_bomb liste=malloc(sizeof(*liste));
 list element=malloc(sizeof(*element));

 if (liste == NULL || element == NULL)
 {
 exit(EXIT_FAILURE);
 }

 element->bomb_x = 0;
 element->bomb_y = 0;

 element->suivant = NULL;
 // liste->premier = element;

 return liste;
 }





 //Ajouter un élément en tête de liste

 void bombeInsertion(list_bomb liste, t_player player)
 {
 //Création du nouvel élément
 list nouveau = malloc(sizeof(*nouveau));
 if (liste == NULL || nouveau == NULL)
 {
 exit(EXIT_FAILURE);
 }

 nouveau->bomb_x= player_get_x(player);
 nouveau->bomb_y= player_get_y(player);

 //Insertion de l'élément au début de la liste
 nouveau->suivant = liste->premier;
 liste -> premier = nouveau;

 }



 //Affichage de la liste chaînée

 void bombeAfficherListe(t_player player, list_bomb liste)
 {
 if (liste == NULL)
 {
 exit(EXIT_FAILURE);
 }

 list actuel = liste->premier;

 while ( actuel != NULL )
 {
 printf("x=%d;y=%d -> ",actuel->bomb_x,actuel->bomb_y);

 actuel=actuel->suivant;
 }
 printf("NULL\n");
 }


 void bombPrint(t_player player, t_map map, list_bomb liste)
 {

 //liste = bombeInitialisation();
 //bombeInsertion(liste,player);

 //bombeAfficherListe(player,liste);

 map_set_cell_type(map, player_get_x(player),player_get_y(player), CELL_BOMB);


 }


 */

/*
 // Test if the list is empty
 int list_is_empty (list l) {
 return !l;
 }


 // Creates a new list with one element
 list list_new (t_player player)
 {
 list nouvelle_bomb = malloc (sizeof(*nouvelle_bomb));
 assert (nouvelle_bomb);
 nouvelle_bomb->bomb_x = player_get_x(player);
 nouvelle_bomb->bomb_y = player_get_y(player);

 nouvelle_bomb->suivant = NULL;
 return nouvelle_bomb;
 }


 // Add a new element at begining of l, returns the new list
 list list_add (list liste, t_player player) {
 list l1 = list_new (player);
 if (list_is_empty(liste))
 return l1;
 l1->suivant = liste;
 return l1;
 }


 // Print one list element
 void print_list_element (int bomb_x,t_player player)
 {

 printf ("x=%d:y=%d ", player_get_x(player),player_get_y(player));
 }


 // Print each element of the list (recursive
 void list_print (list l,t_player player) {
 if (!list_is_empty(l)) {
 print_list_element(l->bomb_x,player);
 list_print(l->suivant,player);
 }
 }

 */

/*
 list bomb_create (t_player player, t_map map) {

 list newlist= malloc(sizeof(struct list_bomb_));   //We create a new list
 bomb bomb= malloc(sizeof(struct bomb_));			//And a new bomb

 int x=player_get_x(player);
 int y=player_get_y(player);



 //int bomb_throw = player_get_bomb_throw(player); //We get the current bomb_throw
 int i=1;

 switch (player_get_current_way(player)) {

 case NORTH:
 bomb->bomb_x=x;    //While check everything, especially if the bomb meets a tree with bomb_ban_explode
 while (map_is_inside(map, x, y - i) )
 {
 i += 1;
 }
 bomb->bomb_y=y - i +1; //We set the bomb position to the last cell OK
 break;

 case SOUTH:
 bomb->bomb_x=x;
 while (map_is_inside(map, x, y + i)) {
 i += 1;
 }
 bomb->bomb_y=y + i -1;
 break;

 case WEST:
 while (map_is_inside(map, x - i, y))
 {i += 1;}
 bomb->bomb_x=x - i + 1;
 bomb->bomb_y=y;
 break;

 case EAST:
 while (map_is_inside(map, x + i, y) )
 {i += 1;}
 bomb->bomb_x=x + i -1;
 bomb->bomb_y=y;
 break;
 }

 map_set_cell_type(map, bomb->bomb_x, bomb->bomb_y, CELL_BOMB); //The cell becomes a CELL_BOMB


 newlist->bomb=bomb;			//The bomb is set in the newlist
 newlist->next = NULL;		//We carefully initialize next

 return newlist;
 }







 */

/*

 struct s_bomber {
 int x, y;
 SDL_Surface* img_bomb; //Load image bomb
 //enum e_way current_way;

 int nb_bomb;
 };

 void bomb_display(t_bomb bomb, SDL_Surface *screen) {
 assert(bomb);
 assert(bomb->img_bomb);
 assert(screen);

 SDL_Rect place;
 place.x = bomb->x * SIZE_BLOC;
 place.y = bomb->y * SIZE_BLOC;

 SDL_BlitSurface(bomb->img_bomb, NULL, screen,&place);
 }
 */
/*

list bomb_create(t_player player, t_map map,list newlist) {
	//,list newlist
	//list newlist = malloc(sizeof(*newlist));   //We create a new list
	//newlist = malloc(sizeof(*newlist));
	t_bomb bomb = malloc(sizeof(*bomb));			//And a new bomb
	//FILE *fichier = NULL;
	int x = player_get_x(player);
	int y = player_get_y(player);



	bomb->bomb_x = x;
	bomb->bomb_y = y;
	//map_set_cell_type(map, bomb->bomb_x, bomb->bomb_y, CELL_BOMB_TTL4); //The cell becomes a CELL_BOMB

	//player_dec_nb_bomb(player);

	newlist->bomb = bomb;
	newlist->next = NULL;
	newlist->bomb->timer = 4000;
	//list_print(newlist);


	// bomb_affichage(player, map, newlist);


	bomb_affichage(player, map, newlist);

	//list_print(newlist);

	//bomb_affichage(player,map,newlist);
	//list_print(newlist);


	//time_t time(time_t *variable);
	//printf("%f.\n", (double) time(NULL));


	 FILE *fichier = NULL;
	 fichier = fopen("bar.txt", "w");
	 fprintf(fichier, "%d", bomb->bomb_x);
	 fclose(fichier);

	//fprintf(fichier,"2");
	//fclose(fichier);
	return newlist;
}
*/

